package com.coin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.Notice;
import com.coin.mapper.NoticeMapper;
import com.coin.service.NoticeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * @author 李力
 * @description 针对表【notice(系统资讯公告信息)】的数据库操作Service实现
 * @createDate 2025-05-24 17:08:03
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
        implements NoticeService {

    @Override
    public Page<Notice> findByPage(Page<Notice> page, String title, String startTime, String endTime, Integer status) {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.like(StrUtil.isNotEmpty(title), Notice::getTitle, title)
                .between(StrUtil.isNotEmpty(startTime) && StrUtil.isNotEmpty(endTime), Notice::getCreated, startTime, endTime);
        return page(page, wrapper);
    }


    /**
     * @Description: 删除公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: ids
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteNotices(String[] ids) {
        if (ids == null || ids.length == 0) {
            throw new RuntimeException("参数为空");
        }
        return removeBatchByIds(Arrays.asList(ids));
    }

    /**
     * @Description: 启用/禁用一个公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: id  status
     * @return: status
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateNotice(Long id, Integer status) {
        if (id == null || status == null) {
            throw new RuntimeException("参数为空");
        }
        Notice notice = getById(id);
        if (notice == null) {
            throw new RuntimeException("公告不存在");
        }
        notice.setStatus(status);
        return updateById(notice);
    }

    /**
     * @Description: 新增一个公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: notice
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addNotice(Notice notice) {
        if (notice == null) {
            throw new RuntimeException("参数为空");
        }
        return save(notice);
    }

    /**
     * @Description: 修改公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: notice
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyNotice(Notice notice) {
        if (notice == null) {
            throw new RuntimeException("参数为空");
        }
        return updateById(notice);
    }

    @Override
    public Page<Notice> simple(Page<Notice> page) {
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper = wrapper.eq(Notice::getStatus, 1)
                .orderByAsc(Notice::getSort);
        return page(page, wrapper);
    }

    /**
     * @Description: 根据Id查询公告详情
     * @Author: 李 力
     * @Date: 2025/6/21
     * @Param:
     * @return: R
     */
    @Override
    public Notice getSimpleDetail(Long id) {
        if (id == null) {
            throw new RuntimeException("参数为空");
        }
        return getById(id);
    }
}




